<?php
/**
 * Created by JetBrains PhpStorm.
 * User: heimuya
 * Date: 13-8-14
 * Time: 下午1:34
 * 后台管理系统控制器基类
 */
class AdminAction extends Action{
    protected $data = array();

    //基类初始化方法
    public function _initialize(){
        if(!session('unick') || !session('uid')){
            $this->redirect('Login/index', array('type' => 3));
        }
        $this->assign('unick', session('unick'));
        $this->assign('uid', session('uid'));
        $this->assign('utype', session('utype'));
        $this->assign('uname', session('uname'));
        $this->assign('ctype', session('csstype'));
		
    }
	
	/**
	 * 检查是否有相同排序号,有则替换，并整体往后启动一位
	 * @param $table 数据表
	 * @param $sequence 排序号
	 * @param $type 操作类型
	 * @param $id 原始Id
	 * @return $sid 返回可以插入的序列号
	 */
	 public function checkSequence($table, $sequence, $type = 'create', $id = null){
	 	$model = D($table);
		$existMaxSequence = $model->max('sequence');
		if($type == 'create'){//新增
			if(intval($sequence) > intval($existMaxSequence) || $sequence == ''){//需要插入的序号大于最大的序号
				return intval($existMaxSequence) + 1;
			}
			//需要插入的序号在现存序号之间，则其后的都顺序加1
			$sequence = intval($sequence);
			$where['sequence'] = array('egt', $sequence);
			$model->where($where)->setInc('sequence');
		}else{
			$oldsequence = $model->field('sequence')->where('id = '.$id)->find();
			$oldsequence = intval($oldsequence['sequence']);
			$sequence = intval($sequence);
			if($type == 'edit'){
				if($oldsequence > $sequence){
					$where['sequence'] = array(array('egt', $sequence), array('lt', $oldsequence), 'and');
					$model->where($where)->setInc('sequence');
				}else{
					$where['sequence'] = array(array('gt', $oldsequence), array('elt', $sequence), 'and');
					$model->where($where)->setDec('sequence');
				}
			}else if($type == 'delete'){
				$where['sequence'] = array('gt', $oldsequence);
				$model->where($where)->setDec('sequence');
			}
		}
		return ($existMaxSequence > $sequence) ? $sequence : $existMaxSequence;
	 }
	
	/**
	 * 分页函数
	 * @param $modelObject 数据模型实例
	 * @param $map 查询条件
	 * @param $order 排序方式
	 * @return $list 查询结果数组
	 */
	 public function pageList($modelObject, $map = array(), $listRows = 20, $order = 'id desc'){
	 	import('ORG.Util.Page');
		$count = $modelObject->where($map)->count();
		$page = new Page($count, $listRows);
		
		$show = $page->show();//分页显示输出
		$this->assign('pageInfo', $show);
		
		$list = $modelObject->where($map)->order($order)->limit($page->firstRow.','.$page->listRows)->select();
		
		return $list;
	 }
	 
	 
	/**
	 * 分页函数(不适用thinkphp自带分页函数，而是采用dataTable的方式)
	 * @param $modelObject 数据模型实例
	 * @param $map 查询条件
	 * @param $field 需要排除的字段
	 * @param $order 排序方式
	 * @return $list 查询结果数组
	 */
	 public function pageList_dataTable($modelObject, $getdata, $map = array(), $field = '', $order = 'id desc'){
	 	$count = $modelObject->where($map)->count();
		if(isset($getdata['iDisplayStart']) && $getdata['iDisplayLength'] != '-1'){
			$listRow = $getdata['iDisplayLength'];
			$firstRow = $getdata['iDisplayStart'];
		}else{
			if($getdata['iDisplayLength'] == '-1'){
				$listRow = $count;
			}else{
				$listRow = 10;
			}
			$firstRow = 0;
		}
		
		
		
		$list = $modelObject->where($map)->field($field, TRUE)->order($order)->limit($firstRow.','.$listRow)->select();
		
		return array('count' => $count, 'list' => $list);
	 }
	 
	
	/**
	 * 修改系统版本号
	 */
	public function systemAbstract(){
		if(!is_writeable(SYSTEM_ROOT_PATH)) {
	        header('Content-Type:text/html; charset=utf-8');
	        exit('目录 [ '.SYSTEM_ROOT_PATH.' ] 不可写！');
	    }
		$filename = SYSTEM_ROOT_PATH.'abstract.txt';
		if(!file_exists($filename)){//判断是否存在abstract文件
			touch($filename);
		}
		$oldabstract = trim(file_get_contents($filename));//原始版本号
		do {
			$newabstract = random_num_str();//10位的abstract
		}while($oldabstract === $newabstract);
		file_put_contents($filename, $newabstract);
		return TRUE;
	}
}